/**
 * Copyright &copy; 2012-2016 <a href="https://www.kaaction.com">KAA</a> All rights reserved All rights reserved.
 */
package com.kaaction.modules.ka.web;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.servlet.mvc.support.RedirectAttributes;

import com.kaaction.common.config.Global;
import com.kaaction.common.persistence.Page;
import com.kaaction.common.web.BaseController;
import com.kaaction.common.utils.StringUtils;
import com.kaaction.modules.ka.entity.KaCategory;
import com.kaaction.modules.ka.service.KaCategoryService;

/**
 * 品类Controller
 * @author huanghaiming
 * @version 2017-06-14
 */
@Controller
@RequestMapping(value = "${adminPath}/ka/kaCategory")
public class KaCategoryController extends BaseController {

	@Autowired
	private KaCategoryService kaCategoryService;
	
	@ModelAttribute
	public KaCategory get(@RequestParam(required=false) String id) {
		KaCategory entity = null;
		if (StringUtils.isNotBlank(id)){
			entity = kaCategoryService.get(id);
		}
		if (entity == null){
			entity = new KaCategory();
		}
		return entity;
	}
	
	@RequiresPermissions("ka:kaCategory:view")
	@RequestMapping(value = {"list", ""})
	public String list(KaCategory kaCategory, HttpServletRequest request, HttpServletResponse response, Model model) {
		Page<KaCategory> page = kaCategoryService.findPage(new Page<KaCategory>(request, response), kaCategory); 
		model.addAttribute("page", page);
		return "modules/ka/kaCategoryList";
	}

	@RequiresPermissions("ka:kaCategory:view")
	@RequestMapping(value = "form")
	public String form(KaCategory kaCategory, Model model) {
		model.addAttribute("kaCategory", kaCategory);
		return "modules/ka/kaCategoryForm";
	}

	@RequiresPermissions("ka:kaCategory:edit")
	@RequestMapping(value = "save")
	public String save(KaCategory kaCategory, Model model, RedirectAttributes redirectAttributes) {
		if (!beanValidator(model, kaCategory)){
			return form(kaCategory, model);
		}
		int cnt = kaCategoryService.getEqual(kaCategory);
		if(cnt>0){
			model.addAttribute("message", "该品类已存在");
			return form(kaCategory, model);
		}
		kaCategoryService.save(kaCategory);
		addMessage(redirectAttributes, "保存品类成功");
		return "redirect:"+Global.getAdminPath()+"/ka/kaCategory/?repage";
	}
	
	@RequiresPermissions("ka:kaCategory:delete")
	@RequestMapping(value = "delete")
	public String delete(KaCategory kaCategory, RedirectAttributes redirectAttributes) {
		kaCategoryService.delete(kaCategory);
		addMessage(redirectAttributes, "删除品类成功");
		return "redirect:"+Global.getAdminPath()+"/ka/kaCategory/?repage";
	}

}